package com.ruicar.afs.cloud.bizcommon.algorithm.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruicar.afs.cloud.bizcommon.algorithm.enums.CalculateType;
import com.ruicar.afs.cloud.bizcommon.algorithm.service.AfsAlgorithmService;
import com.ruicar.afs.cloud.bizcommon.algorithm.service.FinanceCalculatorService;
import com.ruicar.afs.cloud.bizcommon.business.bean.AfsRepaymentInfo;
import com.ruicar.afs.cloud.bizcommon.business.dto.CalculatorDTO;
import com.ruicar.afs.cloud.bizcommon.business.entity.FinRentAdjustDetails;
import com.ruicar.afs.cloud.bizcommon.business.vo.RepaymentPlanVO;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.parameter.commom.enums.CostType;
import com.ruicar.afs.cloud.parameter.commom.enums.DiscountType;
import com.ruicar.afs.cloud.parameter.commom.enums.WhetherEnum;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 金融算法服务类
 * @author: sijun.yu
 * @date: 2020/7/10 14:47
 */
@Slf4j
@Service
@AllArgsConstructor
public class FinanceCalculatorServiceImpl implements FinanceCalculatorService {

    private AfsAlgorithmService afsAlgorithmService;

    /**
     * 合并还款计划表
     *
     * @param list
     * @return
     */
    public List<AfsRepaymentInfo> joinRepaymentPlan(List<AfsRepaymentInfo> list) {

        List<AfsRepaymentInfo> resultList = new ArrayList<AfsRepaymentInfo>();
        if (list != null && list.size() > 0) {
            list.parallelStream().collect(Collectors.groupingBy(p -> p.getNo(), Collectors.toList())).forEach(
                    (id, transfer) -> {
                        transfer.stream().reduce((a, b) -> new AfsRepaymentInfo(
                                a.getNo(),
                                a.getYueGong().add(b.getYueGong()),
                                a.getBenJin().add(b.getBenJin()),
                                a.getLiXi().add(b.getLiXi()),
                                a.getYuE().add(b.getYuE()),
                                a.getMonthlyInterest().add(b.getMonthlyInterest()),
                                a.getCompoundInterest().add(b.getCompoundInterest())
                        )).ifPresent(resultList::add);
                    }
            );
        }
        return resultList;
    }

    /**
     * 单个还款计划表
     *
     * @param condition
     * @return
     */
    public IResponse getSingleRepaymentPlan(CalculatorDTO condition) {

        // 获取还款计划表
        JSONObject result = afsAlgorithmService.calculatePaymentpPlan(condition);
        if (result.containsKey("success") && 1 == result.getInteger("success")) {

            RepaymentPlanVO planVO = new RepaymentPlanVO();
            List<CalculatorDTO> costList = new ArrayList<CalculatorDTO>();
            BigDecimal totalRent = BigDecimal.ZERO;
            BigDecimal totalInterest = BigDecimal.ZERO;
            List<AfsRepaymentInfo> repaymentList = new ArrayList<AfsRepaymentInfo>();
            BigDecimal maxRent = BigDecimal.ZERO;

            JSONArray planList = result.getJSONArray("data");
            for (int i = 0; i < planList.size(); i++) {
                AfsRepaymentInfo repaymentInfo = JSONObject.toJavaObject(planList.getJSONObject(i), AfsRepaymentInfo.class);
                BigDecimal yueGong = repaymentInfo.getYueGong();
                // 第一期
                if (1 == repaymentInfo.getNo()) {
                    condition.setFirstRent(yueGong);
                    if (CalculateType.arbitraryloan.getValue().equals(condition.getAlgorithmType())) {
                        condition.setCostType(CostType.CARAMT.getIndex());
                    }
                }
                if (maxRent.compareTo(yueGong) < 0) {
                    maxRent = yueGong;
                }
                repaymentList.add(repaymentInfo);
                totalRent = totalRent.add(yueGong);
                totalInterest = totalInterest.add(repaymentInfo.getLiXi());
            }
            condition.setDiscountAmt(BigDecimal.ZERO);
            condition.setTotalRent(totalRent);
            condition.setTotalInterest(totalInterest);
            costList.add(condition);

            planVO.setCostList(costList);
            planVO.setRepaymentList(repaymentList);
            planVO.setMaxRent(maxRent);
            planVO.setTotalDiscountAmt(BigDecimal.ZERO);
            planVO.setTotalInterest(totalInterest);
            planVO.setTotalRent(totalRent);
            planVO.setRepaymentList(repaymentList);
            return IResponse.success(planVO);
        } else {
            String message = result.getString("message");
            return IResponse.fail("code:" + result.getString("code") + ",message:" + message);
        }
    }

    /**
     * 获取客户利率（月） 只舍不入，保留一位小数
     * @param dto
     * @return
     */
    public BigDecimal getMonthlyRate(CalculatorDTO dto){

        BigDecimal loanTerm = new BigDecimal(dto.getLoanTerm());
        BigDecimal monthlyRate = (dto.getFirstRent().multiply(loanTerm).subtract(dto.getLoanAmt())).multiply(new BigDecimal("1000")).divide(loanTerm.multiply(dto.getLoanAmt()),1,BigDecimal.ROUND_DOWN);
        dto.setMonthlyRate(monthlyRate);
        return monthlyRate;
    }

    /**
     * 万元系数法，只舍不入，保留整数
     * @param dto
     * @return
     */
    public BigDecimal getMyriadCoefficient(CalculatorDTO dto){

        BigDecimal myriadCoefficient = new BigDecimal("10000").multiply(dto.getFirstRent()).divide(dto.getLoanAmt(),0,BigDecimal.ROUND_DOWN);
        dto.setMyriadCoefficient(myriadCoefficient);
        return myriadCoefficient;
    }

    /**
     * 多个还款计划表
     *
     * @param list
     * @return
     */
    public IResponse getMultipleRepaymentPlan(List<CalculatorDTO> list) {

        RepaymentPlanVO planVO = new RepaymentPlanVO();
        List<CalculatorDTO> costList = new ArrayList<CalculatorDTO>();
        List<AfsRepaymentInfo> repaymentList = new ArrayList<AfsRepaymentInfo>();
        BigDecimal totalRent = BigDecimal.ZERO;
        BigDecimal totalInterest = BigDecimal.ZERO;
        BigDecimal totalDiscountAmt = BigDecimal.ZERO;
        BigDecimal maxRent = BigDecimal.ZERO;
        // 遍历车款和附加贷
        for (int i = 0; i < list.size(); i++) {

            CalculatorDTO condition = list.get(i);
            BigDecimal currentRent = BigDecimal.ZERO;
            BigDecimal sumRent = BigDecimal.ZERO;
            BigDecimal sumInterest = BigDecimal.ZERO;
            BigDecimal discountAmt = BigDecimal.ZERO;
            // 调用贴息金额接口
            if (DiscountType.NORMAL.getIndex().equals(condition.getDiscountType())) {
                JSONObject discountJson = afsAlgorithmService.calculateDiscount(condition);
                discountAmt = discountJson.getBigDecimal("data");
            }
            // 灵活贴息
            else if (DiscountType.NIMBLE.getIndex().equals(condition.getDiscountType())) {
                discountAmt = condition.getDiscountAmt();
            }

            // 调用还款计划接口
            Assert.isTrue(condition.getCustRate()!=null,"客户利率为空，请输入正确产品期数");
            JSONObject planJson = afsAlgorithmService.calculatePaymentpPlan(condition);
            JSONArray planList = planJson.getJSONArray("data");
            if (planList != null) {
                List<AfsRepaymentInfo> firstList = new ArrayList<AfsRepaymentInfo>();
                BigDecimal firstRent = BigDecimal.ZERO;
                Boolean flag = Boolean.TRUE;
                for (int j = 0; j < planList.size(); j++) {
                    AfsRepaymentInfo repaymentInfo = JSONObject.toJavaObject(planList.getJSONObject(j), AfsRepaymentInfo.class);
                    BigDecimal yueGong = repaymentInfo.getYueGong();
                    // 前几期非零租金
                    if (yueGong.compareTo(BigDecimal.ZERO)>0 && flag) {
                        firstRent = yueGong;
                        flag = Boolean.FALSE;
                    }
                    // 最大租金
                    if (currentRent.compareTo(yueGong) < 0) {
                        currentRent = yueGong;
                    }
                    sumRent = sumRent.add(yueGong);
                    sumInterest = sumInterest.add(repaymentInfo.getLiXi());
                    firstList.add(repaymentInfo);
                    repaymentList.add(repaymentInfo);
                }
                condition.setFirstRent(firstRent);
                condition.setMonthPayAmt(firstRent);
                condition.setTotalInterest(sumInterest);
                condition.setTotalRent(sumRent);
                condition.setDiscountAmt(discountAmt);
                condition.setRepaymentList(firstList);
                condition.setMaxRent(currentRent);
                if(CalculateType.equalrental.getValue().equals(condition.getAlgorithmType())){
                    getMonthlyRate(condition);// 客户利率（月）
                    getMyriadCoefficient(condition);// 万元系数法
                }
                costList.add(condition);
            }
            maxRent = maxRent.add(currentRent);
            totalInterest = totalInterest.add(sumInterest);
            totalRent = totalRent.add(sumRent);
            totalDiscountAmt = totalDiscountAmt.add(discountAmt);
        }
        planVO.setTotalDiscountAmt(totalDiscountAmt);
        planVO.setCostList(costList);
        if (list.size() <= 1) {
            planVO.setRepaymentList(repaymentList);
        } else {// 合并还款计划表
            planVO.setRepaymentList(this.joinRepaymentPlan(repaymentList));
        }
        planVO.setTotalRent(totalRent);
        planVO.setTotalInterest(totalInterest);
        planVO.setMaxRent(maxRent);

        return IResponse.success(planVO);
    }

    /**
     * 合并摊销计划表
     *
     * @param list
     * @return
     */
    public List<AfsRepaymentInfo> joinAmortizationPlan(List<AfsRepaymentInfo> list) {

        List<AfsRepaymentInfo> resultList = new ArrayList<AfsRepaymentInfo>();
        if (list != null && list.size() > 0) {
            list.parallelStream().collect(Collectors.groupingBy(p -> p.getNo(), Collectors.toList())).forEach(
                    (id, transfer) -> {
                        transfer.stream().reduce((a, b) -> new AfsRepaymentInfo(
                                a.getNo(),
                                a.getExecutionDate(),
                                a.getDueDate(),
                                a.getChargeAmortizeAmt().add(b.getChargeAmortizeAmt()),
                                a.getChargeOutstandingAmt().add(b.getChargeOutstandingAmt()),
                                a.getCharegOpeningBalAmt().add(b.getCharegOpeningBalAmt())
                        )).ifPresent(resultList::add);
                    }
            );
        }
        return resultList;
    }

    /**
     * 多个摊销计划表 - 针对贴息
     *
     * @param list
     * @return
     */
    public IResponse getMultipleAmortizationPlan(List<CalculatorDTO> list) {

        RepaymentPlanVO planVO = new RepaymentPlanVO();
        List<CalculatorDTO> costList = new ArrayList<CalculatorDTO>();
        List<AfsRepaymentInfo> repaymentList = new ArrayList<AfsRepaymentInfo>();
        // 遍历车款和附加贷
        for (CalculatorDTO condition : list) {
            // 无贴息不调用摊销计划
            if(DiscountType.NO.getIndex().equals(condition.getDiscountType())||(condition.getDiscountAmt().compareTo(BigDecimal.ZERO)!=1)){
                continue;
            }
            // 调用摊销计划接口
            JSONObject planJson = afsAlgorithmService.getAmortizationMonthPlan(condition);
            JSONArray planList = planJson.getJSONArray("data");
            if (planList != null) {
                List<AfsRepaymentInfo> firstList = new ArrayList<AfsRepaymentInfo>();
                for (int i = 0; i < planList.size(); i++) {
                    AfsRepaymentInfo amortizationPlan = JSONObject.toJavaObject(planList.getJSONObject(i), AfsRepaymentInfo.class);
                    firstList.add(amortizationPlan);
                    repaymentList.add(amortizationPlan);
                }
                condition.setRepaymentList(firstList);
                costList.add(condition);
            }
        }
        planVO.setCostList(costList);
        if (list.size() <= 1) {
            planVO.setRepaymentList(repaymentList);
        } else {// 合并还款计划表
            planVO.setRepaymentList(this.joinAmortizationPlan(repaymentList));
        }
        return IResponse.success(planVO);
    }

    /**
     * 合并收入计划表
     * @param list
     * @return
     */
    public List<AfsRepaymentInfo> joinIncomPlan(List<AfsRepaymentInfo> list) {

        List<AfsRepaymentInfo> resultList = new ArrayList<AfsRepaymentInfo>();
        if (list != null && list.size() > 0) {
            list.parallelStream().collect(Collectors.groupingBy(p -> p.getNo(), Collectors.toList())).forEach(
                    (id, transfer) -> {
                        transfer.stream().reduce((a, b) -> new AfsRepaymentInfo(
                                a.getNo(),
                                a.getInComAMT().add(b.getInComAMT()),
                                a.getInComeDD(),
                                a.getExecutionDate(),
                                a.getPostingDate()
                        )).ifPresent(resultList::add);
                    }
            );
        }
        return resultList;
    }

    /**
     * 多个收入计划表
     * @param list
     * @return
     */
    public IResponse getMultipleIncomePlan(List<CalculatorDTO> list){

        RepaymentPlanVO planVO = new RepaymentPlanVO();
        List<CalculatorDTO> costList = new ArrayList<CalculatorDTO>();
        List<AfsRepaymentInfo> repaymentList = new ArrayList<AfsRepaymentInfo>();
        // 遍历车款和附加贷
        for (CalculatorDTO condition : list) {
            JSONObject planJson = afsAlgorithmService.getIncomePlan(condition);
            JSONArray planList = planJson.getJSONArray("data");
            if (planList != null) {
                List<AfsRepaymentInfo> firstList = new ArrayList<AfsRepaymentInfo>();
                for(int i=0; i < planList.size(); i++){
                    AfsRepaymentInfo incomePlan = JSONObject.toJavaObject(planList.getJSONObject(i), AfsRepaymentInfo.class);
                    firstList.add(incomePlan);
                    repaymentList.add(incomePlan);
                }
                condition.setProductId(condition.getProductId());
                condition.setCostType(condition.getCostType());
                condition.setRepaymentList(firstList);
                costList.add(condition);
            }
        }
        planVO.setCostList(costList);
        if (list.size() <= 1) {
            planVO.setRepaymentList(repaymentList);
        } else {// 合并还款计划表
            planVO.setRepaymentList(this.joinIncomPlan(repaymentList));
        }
        return IResponse.success(planVO);
    }

    /**
     * 获取产品参数信息
     *
     * @param dataJson
     * @param carProuctDto
     * @param addProductDto
     */
    public List<CalculatorDTO> getProductParameter(JSONObject dataJson, CalculatorDTO carProuctDto, CalculatorDTO addProductDto) {

        List<CalculatorDTO> resuletList = new ArrayList<CalculatorDTO>();
        BigDecimal hundred = new BigDecimal("100");
        // 主产品
        String isAdditional = dataJson.getString("isAdditional");
        JSONObject mainProductInfo = dataJson.getJSONObject("mainProductInfo");
        JSONObject addProductInfo = dataJson.getJSONObject("addProductInfo");
        if (mainProductInfo != null) {
            String algorithmType = carProuctDto.getAlgorithmType();
            if(StringUtils.isEmpty(algorithmType)){
                algorithmType = mainProductInfo.getString("algorithmType");
            }
            // 分段贷
            if (CalculateType.ballonloan.getValue().equals(algorithmType)) {
                String isTail = mainProductInfo.getString("isTail");
                JSONObject repaymentData = mainProductInfo.getJSONObject("repaymentData");
                JSONObject repaymentMethods = repaymentData.getJSONObject("repaymentMethods");
                carProuctDto.setBallonLoanType(repaymentMethods.getString("finalPaymentCalculationLogic"));
                // 非尾款产品-多段
                if (WhetherEnum.No.getIndex().equals(isTail)) {
                    JSONArray data = repaymentData.getJSONArray("data");
                    // 拷贝LIST
                    List<FinRentAdjustDetails> rentAdjustDetailsList = new ArrayList<FinRentAdjustDetails>();
                    String finalPaymentBenchmark = repaymentMethods.getString("finalPaymentBenchmark");
                    for(FinRentAdjustDetails vo:data.toJavaList(FinRentAdjustDetails.class)){
                        FinRentAdjustDetails rentAdjustDetails = new FinRentAdjustDetails();
                        BeanUtils.copyProperties(vo,rentAdjustDetails);
                        // 按比例
                        if(!WhetherEnum.YES.getIndex().equals(vo.getPaymentByStages())){
                            // 车价* 比例
                            if ("0".equals(finalPaymentBenchmark)) {
                                rentAdjustDetails.setRepaymentAmountStages(vo.getProportionRepaymentSections().multiply(carProuctDto.getContractAmt()).divide(hundred, 2, BigDecimal.ROUND_HALF_DOWN));
                            }
                            // 贷款金额*比例
                            else if ("1".equals(finalPaymentBenchmark)) {
                                rentAdjustDetails.setRepaymentAmountStages(vo.getProportionRepaymentSections().multiply(carProuctDto.getLoanAmt()).divide(hundred, 2, BigDecimal.ROUND_HALF_DOWN));
                            }
                            // 合同价格*比例
                            else if ("2".equals(finalPaymentBenchmark)) {
                                // 同一个产品时
                                if ("1".equals(isAdditional) && addProductInfo != null) {
                                    String isProduct = addProductInfo.getJSONObject("extrasInfo").getString("isProduct");
                                    if ("0".equals(isProduct)) {
                                        rentAdjustDetails.setRepaymentAmountStages(vo.getProportionRepaymentSections().multiply(carProuctDto.getContractAmt().add(carProuctDto.getAddAmt())).divide(hundred, 2, BigDecimal.ROUND_HALF_DOWN));
                                    }else{
                                        rentAdjustDetails.setRepaymentAmountStages(vo.getProportionRepaymentSections().multiply(carProuctDto.getContractAmt()).divide(hundred, 2, BigDecimal.ROUND_HALF_DOWN));
                                    }
                                }
                            }
                            // 车辆融资额*比例
                            else if ("3".equals(finalPaymentBenchmark)) {
                                rentAdjustDetails.setRepaymentAmountStages(vo.getProportionRepaymentSections().multiply(carProuctDto.getContractAmt()).multiply(hundred.subtract(carProuctDto.getDownPayScale()))
                                        .divide(hundred, 2, BigDecimal.ROUND_HALF_DOWN).divide(hundred,2, BigDecimal.ROUND_HALF_DOWN));
                            }
                        }
                        rentAdjustDetailsList.add(rentAdjustDetails);
                    }
                    carProuctDto.setPeroids(rentAdjustDetailsList.stream().map(x -> String.valueOf(x.getNumberInstallments())).collect(Collectors.joining(",")));
                    carProuctDto.setMoney(rentAdjustDetailsList.stream().map(x -> x.getRepaymentAmountStages().toString()).collect(Collectors.joining(",")));
                } else {
                    carProuctDto.setPeroids(String.valueOf(carProuctDto.getLoanTerm()));
                    carProuctDto.setMoney(String.valueOf(carProuctDto.getTailPayAmt()));
                }
                carProuctDto.setIsTail(isTail);
            }
            // 等比累进
            else if (CalculateType.equalratioret.getValue().equals(algorithmType)) {
                JSONObject repaymentMethods = mainProductInfo.getJSONObject("repaymentData").getJSONObject("repaymentMethods");
                carProuctDto.setStep(repaymentMethods.getInteger("numberStepsPerStep"));
                carProuctDto.setStepPercent(repaymentMethods.getBigDecimal("floatingProportionStepLoan"));
            }
            // 结构化还款
            else if (CalculateType.structuredloan.getValue().equals(algorithmType)) {
                JSONObject repaymentMethods = mainProductInfo.getJSONObject("repaymentData").getJSONObject("repaymentMethods");
                carProuctDto.setStructuredMonth(repaymentMethods.getInteger("setAnyNumberCredits"));
                carProuctDto.setStructuredType(repaymentMethods.getString("paymentType"));
                carProuctDto.setStructuredMoney(repaymentMethods.getBigDecimal("monthlySupplyAmount"));
            }
            // 任意贷
            else if(CalculateType.arbitraryloan.getValue().equals(algorithmType)){
                JSONObject repaymentMethods = mainProductInfo.getJSONObject("repaymentData").getJSONObject("repaymentMethods");
                carProuctDto.setSetAnyNumberCredits(repaymentMethods.getInteger("setAnyNumberCredits"));
                String adjustedMinimumMode = repaymentMethods.getString("adjustedMinimumMode");
                if("0".equals(adjustedMinimumMode)){// 0-比例
                    carProuctDto.setAdjustedMonthlyPayments(repaymentMethods.getBigDecimal("monthlyPaymentPercentage").multiply(carProuctDto.getLoanAmt()).divide(hundred, 2, BigDecimal.ROUND_HALF_DOWN));
                }else{// 1-金额
                    carProuctDto.setAdjustedMonthlyPayments(repaymentMethods.getBigDecimal("adjustedMonthlyPayments"));
                }
                carProuctDto.setAdjustedMinimumMode(adjustedMinimumMode);
                String firstNMinimumLimit = repaymentMethods.getString("firstNMinimumLimit");
                if("0".equals(firstNMinimumLimit)){// 0-比例
                    carProuctDto.setTotalAmountInstalments(repaymentMethods.getBigDecimal("percentageTotalRepayment").multiply(carProuctDto.getLoanAmt()).divide(hundred, 2, BigDecimal.ROUND_HALF_DOWN));
                }else{// 1-金额
                    carProuctDto.setTotalAmountInstalments(repaymentMethods.getBigDecimal("totalAmountInstalments"));
                }
                carProuctDto.setFirstNMinimumLimit(firstNMinimumLimit);
            }
            carProuctDto.setAlgorithmType(algorithmType);
            if(StringUtils.isEmpty(carProuctDto.getDiscountType())){
                carProuctDto.setDiscountType(mainProductInfo.getString("discountOption"));
            }
            carProuctDto.setOverdueBase(mainProductInfo.getJSONObject("detailInfo").getString("overdueRate"));// 逾期基数
            resuletList.add(carProuctDto);
        }

        // 有附加贷
        if (WhetherEnum.YES.getIndex().equals(isAdditional) && addProductInfo != null && addProductDto != null) {
            String algorithmType = addProductDto.getAlgorithmType();
            if(StringUtils.isEmpty(algorithmType)){
                algorithmType = addProductInfo.getString("algorithmType");
            }
            // 分段贷
            if (CalculateType.ballonloan.getValue().equals(algorithmType)) {
                String isTail = addProductInfo.getString("isTail");
                JSONObject repaymentData = addProductInfo.getJSONObject("repaymentData");
                JSONObject repaymentMethods = repaymentData.getJSONObject("repaymentMethods");
                addProductDto.setBallonLoanType(repaymentMethods.getString("finalPaymentCalculationLogic"));
                // 非尾款产品-多段
                if (WhetherEnum.No.getIndex().equals(isTail)) {
                    JSONArray data = repaymentData.getJSONArray("data");
                    List<FinRentAdjustDetails> rentAdjustDetailsList = new ArrayList<FinRentAdjustDetails>();
                    String finalPaymentBenchmark = repaymentMethods.getString("finalPaymentBenchmark");
                    for(FinRentAdjustDetails vo:data.toJavaList(FinRentAdjustDetails.class)){
                        FinRentAdjustDetails rentAdjustDetails = new FinRentAdjustDetails();
                        BeanUtils.copyProperties(vo,rentAdjustDetails);
                        // 按比例
                        if(!WhetherEnum.YES.getIndex().equals(vo.getPaymentByStages())){
                            // 0-附加金额* 比例，2-合同价格*比例
                            if ("0".equals(finalPaymentBenchmark) || "2".equals(finalPaymentBenchmark)) {
                                rentAdjustDetails.setRepaymentAmountStages(vo.getProportionRepaymentSections().multiply(addProductDto.getContractAmt()).divide(hundred, 2, BigDecimal.ROUND_HALF_DOWN));
                            }
                            // 1-贷款金额*比例，3-车辆融资额*比例
                            else if ("1".equals(finalPaymentBenchmark) || "3".equals(finalPaymentBenchmark)) {
                                rentAdjustDetails.setRepaymentAmountStages(vo.getProportionRepaymentSections().multiply(addProductDto.getLoanAmt()).divide(hundred, 2, BigDecimal.ROUND_HALF_DOWN));
                            }
                        }
                        rentAdjustDetailsList.add(rentAdjustDetails);
                    }
                    addProductDto.setPeroids(rentAdjustDetailsList.stream().map(x -> String.valueOf(x.getNumberInstallments())).collect(Collectors.joining(",")));
                    addProductDto.setMoney(rentAdjustDetailsList.stream().map(x -> x.getRepaymentAmountStages().toString()).collect(Collectors.joining(",")));
                } else {
                    addProductDto.setPeroids(String.valueOf(addProductDto.getLoanTerm()));
                    addProductDto.setMoney(String.valueOf(addProductDto.getTailPayAmt()));
                }
            }
            // 等比累进
            else if (CalculateType.equalratioret.getValue().equals(algorithmType)) {
                JSONObject repaymentMethods = addProductInfo.getJSONObject("repaymentData").getJSONObject("repaymentMethods");
                addProductDto.setStep(repaymentMethods.getInteger("numberStepsPerStep"));
                addProductDto.setStepPercent(repaymentMethods.getBigDecimal("floatingProportionStepLoan"));
            }
            // 结构化还款
            else if (CalculateType.structuredloan.getValue().equals(algorithmType)) {
                JSONObject repaymentMethods = addProductInfo.getJSONObject("repaymentData").getJSONObject("repaymentMethods");
                addProductDto.setStructuredMonth(repaymentMethods.getInteger("setAnyNumberCredits"));
                addProductDto.setStructuredType(repaymentMethods.getString("paymentType"));
                addProductDto.setStructuredMoney(repaymentMethods.getBigDecimal("monthlySupplyAmount"));
            }
            addProductDto.setAlgorithmType(algorithmType);
            if(StringUtils.isEmpty(addProductDto.getDiscountType())){
                addProductDto.setDiscountType(addProductInfo.getString("discountOption"));
            }
            resuletList.add(addProductDto);
        }
        return resuletList;
    }

}
